package cn.arikeky.org.controller;


import cn.arikeky.basic.anno.PreAuthorize;
import cn.arikeky.basic.config.BussinessException;
import cn.arikeky.basic.util.AjaxResult;
import cn.arikeky.basic.util.LoginContext;
import cn.arikeky.basic.util.PageList;
import cn.arikeky.org.domain.Employee;
import cn.arikeky.org.domain.Shop;
import cn.arikeky.org.query.EmployeeQuery;
import cn.arikeky.org.service.IEmployeeService;
import cn.arikeky.org.service.IShopService;
import cn.arikeky.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/empl")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {


    @Autowired
    private IEmployeeService EmployeeService;

    @Autowired
    private IShopService shopService;

    /**
     * 添加/修改
     */
    @PutMapping
    @PreAuthorize(name = "empl添加/修改部门数据", sn = "empl:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Employee Employee){
        if (null == Employee.getId()){
            // 添加
            EmployeeService.add(Employee);
        }else {
            // 修改
            EmployeeService.update(Employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "empl删除部门数据", sn = "empl:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            EmployeeService.delete(id);
        } catch(BussinessException ex){
            ex.printStackTrace();
            return AjaxResult.me().fail("参数不能为空");
        }

        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有部门数据" )
    @PreAuthorize(name = "empl查询所有部门数据", sn = "empl:findAll")
    public List<Employee> findAll(){
        return EmployeeService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return EmployeeService.findById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return EmployeeService.queryByPage(query);
    }
    /**
     * 批量删除
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除", sn = "empl:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        EmployeeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /**
     * 根据店铺Id查询员工
     */
    @GetMapping("empList")
    public List<Employee> empList(HttpServletRequest request){
        Logininfo loginUser = LoginContext.getLoginUser(request);
        Employee emp = EmployeeService.findByLogId(loginUser.getId());
                return EmployeeService.empList(emp.getShopId());
    }
}
